package com.rcl.httpurlconnection.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.rcl.httpurlconnection.R;
import com.rcl.httpurlconnection.base.BaseApplication;
import com.rcl.httpurlconnection.bean.ItemBean;

import java.util.ArrayList;
import java.util.List;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/10/22.
 * PS: Not easy to write code, please indicate.
 */
public class ItemListAdapter extends RecyclerView.Adapter<ItemListAdapter.InnerView> {

    private List<ItemBean.DataBean> mList=new ArrayList<>();
    private Context mContext;

    @NonNull
    @Override
    public InnerView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext=parent.getContext();
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page, parent, false);
        InnerView innerView = new InnerView(inflate);
        return innerView;
    }

    @Override
    public void onBindViewHolder(@NonNull InnerView holder, int position) {
        ItemBean.DataBean dataBean = mList.get(position);
        holder.tv_title.setText(dataBean.getTitle());
        if (!TextUtils.isEmpty(dataBean.getImage())) {
            Glide.with(mContext).load(dataBean.getImage()).into(holder.iv_cover);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<ItemBean.DataBean> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public class InnerView extends RecyclerView.ViewHolder {

        private ImageView iv_cover;
        private TextView tv_title;

        public InnerView(@NonNull View itemView) {
            super(itemView);
            iv_cover = itemView.findViewById(R.id.iv_cover);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }
}
